//
//  ShaixuanCell.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/3/2.
//  Copyright © 2016年 game. All rights reserved.
//

#import "ShaixuanCell.h"
#import "ValueModel.h"
#import "Masonry.h"
@interface ShaixuanCell ()

@property (nonatomic,strong) UIView *backView;
@end

@implementation ShaixuanCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
       
        
    }
    return self;
}

-(UILabel *)title{

    if (!_title) {
        _title = [[UILabel alloc]initWithFrame:CGRectMake(25, 8, 60, 25)];
        _title.font = font(SCREEN_WIDTH*0.04);
        
        [self.contentView addSubview:_title];
    }

    return _title;
}


-(void)setModel:(IslanDistance_typeModel *)model{

    _model = model;
    
    self.title.text = _model.name;
    
    if ([_model.name isEqualToString:@"收费"]) {
        
        self.selectTitle = @"charge_type";
        
    }else if ([_model.name isEqualToString:@"类型"]){
    
        self.selectTitle = @"type";
        
    }else if([_model.name isEqualToString:@"显示"]){
        
        self.selectTitle = @"show";
    }
    
    
    
    [_backView removeFromSuperview];
    _backView = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/4, 0, SCREEN_WIDTH*3/4, 0)];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
    [_backView addGestureRecognizer:tap];
    
    [self.contentView addSubview:_backView];
    
    CGFloat xd = 10;
    CGFloat yd = 8;
    
    for (int i=0; i<_model.value.count; i++) {
        
        NSInteger row = i%3;
        NSInteger lod = i/3;
        
        CGFloat ww = (SCREEN_WIDTH*3/4 -xd*4)/3;
        CGFloat hh = (SCREEN_WIDTH/3-yd*4)/3;
        
        CGFloat xx =  xd +(xd + ww)*row;
        CGFloat yy = yd+(yd + hh)*lod;
        
        UIButton *bnt = [UIButton buttonWithType:UIButtonTypeCustom];
        bnt.frame = CGRectMake(xx, yy, ww, hh);
        
        bnt.tag = i+1000;
        
        
        ValueModel *value = _model.value[i];
        
        [bnt setTitle:value.name forState:UIControlStateNormal];
        
        if (i==0) {
            self.selectString = [NSString stringWithFormat:@"%d",i];
        }
        
        bnt.layer.borderWidth = 0.5;
        [bnt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        bnt.layer.borderColor = [UIColor clearColor].CGColor;
        bnt.titleLabel.font = font(SCREEN_WIDTH*0.04);
        
        if (i==0) {
            bnt.layer.borderColor = mainColor.CGColor;
            [bnt setTitleColor:mainColor forState:UIControlStateNormal];
        }
        
        [bnt addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.backView addSubview:bnt];
        self.cellhight  = Height(bnt)+yd;
    }

    self.backView.height = self.cellhight;
    
    [self BottomLineCreat];
}


-(void)BottomLineCreat{

    UIView *lin = [UIView new];
    lin.backgroundColor  = [UIColor lightGrayColor];
    [self.backView addSubview:lin];
    [lin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.contentView);
        make.height.equalTo(@0.5);
    }];
}

-(void)btnClick:(UIButton *)btn{
    
    for (int i=0; i<self.model.value.count; i++) {
        
        UIButton *bbb = [self.contentView.superview viewWithTag:i+1000];
        
        bbb.layer.borderColor = [UIColor clearColor].CGColor;
        [bbb setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    
    btn.layer.borderColor = mainColor.CGColor;
    [btn setTitleColor:mainColor forState:UIControlStateNormal];
    
    self.selectString = [NSString stringWithFormat:@"%ld",btn.tag-1000];
}

@end
